package com.ainiyar.springweb.config.security.handler;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**************************************
 * File Name : CustomAccessHandler
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/5  01:25
 * Desc :认证用户访问无权限资源时处理器类
 ***************************************/
@Component
public class CustomAccessHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //设置响应编码格式
        response.setContentType("application/json;charset=utf-8");
        ResultResponse<Object> result=ResultResponse.Fail("无权限操作、请联系管理员！", 403);
        WebUtil.renderString(response,result);
    }
}

